//
//  AppDelegate.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/4.
//

#import "AppDelegate.h"
#import "AppDelegate+AppService.h"
#import "SJRotationManager.h"
#import "SDImageCacheConfig.h"
#import "SDImageCache.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self setupLanguage];
    [self initService];
    [self initWindow];
    //iphone 6手机在电站列表里加载图片会内存暴涨
    [SDImageCacheConfig defaultCacheConfig].maxMemoryCount = 20;
    [SDImageCacheConfig defaultCacheConfig].maxMemoryCost = 20*1024;
//    //当前地区时间
//    NSDate *currentDate = [NSDate date];
//    // 获得系统时区
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    //得到源日期与世界标准时间的偏移量
//    NSInteger interval = [zone secondsFromGMTForDate: currentDate];
//    //返回以当前NSDate对象为基准，偏移多少秒后得到的新NSDate对象
//    NSDate *localeDate = [currentDate dateByAddingTimeInterval: interval];
//    
//    
//    NSLog(@"----->当前时间:%@",localeDate);
//    NSTimeZone *local_zone = [NSTimeZone localTimeZone];
//    
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//    dateFormatter.dateFormat = @"Z";
//    NSString *curDateString = [dateFormatter stringFromDate:currentDate];
//    
//    NSLog(@"----->当前时区:%@",local_zone.name);
//    NSTimeInterval timeinteval_GMT = [local_zone secondsFromGMTForDate:currentDate];
//    NSLog(@"--->当前时区跟GMT相差%f  %ld个小时",timeinteval_GMT,(NSInteger)timeinteval_GMT/60/60/24);
//    NSDate *GMT_date = [currentDate dateByAddingTimeInterval:timeinteval_GMT];
//    //当前时区的时间戳
//    NSTimeInterval current_interval = [currentDate timeIntervalSince1970];
//    NSLog(@"--->当前时区的时间戳:%f ",current_interval);
//    //格林尼治时间戳
//    NSTimeInterval gmt_interval = current_interval - timeinteval_GMT;
//    NSLog(@"--->格林尼治时间戳:%f",gmt_interval);
//    
//    NSTimeZone *beijin_zone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
//    NSTimeInterval timeinteval_beijin = [beijin_zone secondsFromGMTForDate:GMT_date];
//    NSLog(@"--->北京时区跟GMT相差%f  %ld个小时",timeinteval_beijin,((NSInteger)timeinteval_beijin)/60/60/24);
//    //北京时间的时间戳
//    NSTimeInterval des_interval = gmt_interval + timeinteval_beijin;
//    NSLog(@"--->北京时区的时间戳%f",des_interval);
//    //转成北京时间
//    NSDate *beijin_date = [NSDate dateWithTimeIntervalSince1970:des_interval];
//    NSLog(@"转成目标时间后:%@",beijin_date);
//    
//    //墨西哥时间
//    NSString *moxige = @"2024-11-13 08:37:59";
//    NSLog(@"----->北京时间:%@",moxige);
//    NSString *beijinStr = [NSDate beiJinTimeConvertToLocalTime:moxige];
//    NSLog(@"----->转换后的时间:%@",beijinStr);

    
    
    
    return YES;
}

-(NSString *)localZoneTimeConvertToBeiJinTime:(NSString*)localTime
{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *dateFormatted = [format dateFromString:localTime];
    format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    //转成UTC时间
    NSString *UTC_dateString = [format stringFromDate:dateFormatted];
    
    NSDate *utcDate = [format dateFromString:UTC_dateString];
    format.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSString *dateString = [format stringFromDate:utcDate];
    return dateString;
}

/**
 将本地日期字符串转为UTC日期字符串
 eg: 2017-10-25 02:07:39 -> 2017-10-24 18:07:39
 */
- (NSString *)getUTCStrFormateLocalStr:(NSString *)localStr {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *dateFormatted = [format dateFromString:localStr];
    format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSString *dateString = [format stringFromDate:dateFormatted];
    return dateString;
}
/**
 将UTC日期字符串转为本地时间字符串
 eg: 2017-10-25 02:07:39  -> 2017-10-25 10:07:39
 */
- (NSString *)getLocalDateFormateUTCDate:(NSString *)utcStr {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSDate *utcDate = [format dateFromString:utcStr];
    format.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSString *dateString = [format stringFromDate:utcDate];
    return dateString;
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
    if (self.window) {
            if (url) {
                NSString *fileName = url.lastPathComponent; // 从路径中获得完整的文件名（带后缀）
                // path 类似这种格式：file:///private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
                NSString *path = url.absoluteString; // 完整的url字符串
                path = [self URLDecodedString:path]; // 解决url编码问题

                NSMutableString *string = [[NSMutableString alloc] initWithString:path];

                if ([path hasPrefix:@"file://"]) { // 通过前缀来判断是文件
                    // 去除前缀：/private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
                    [string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];

                    // 此时获取到文件存储在本地的路径，就可以在自己需要使用的页面使用了
                NSDictionary *dict = @{@"fileName":fileName,
                                           @"filePath":string};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PdfFileUpdate" object:nil userInfo:dict];

                    return YES;
                }
            }
        }
    return YES;
}
// 当文件名为中文时，解决url编码问题
- (NSString *)URLDecodedString:(NSString *)str {
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

    return decodedString;
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft ||
        [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight) {
        //NSLog(@"AppDelegate---支持旋转");
        //return UIInterfaceOrientationMaskAllButUpsideDown;
        if (_allowRotate == 0) {
            NSLog(@"---->不允许旋转,%@",[self orientationString:UIInterfaceOrientationMaskPortrait]);
            return UIInterfaceOrientationMaskPortrait;;
        }
        
        
        return UIInterfaceOrientationMaskLandscape;
    }else{
        if (_allowRotate == 1) {
            NSLog(@"---->允许旋转,%@",[self orientationString:UIInterfaceOrientationMaskLandscape]);
                return UIInterfaceOrientationMaskLandscape;
            }
        //NSLog(@"AppDelegate---仅支持竖屏");
        
        NSLog(@"---->不允许旋转,%@",[self orientationString:UIInterfaceOrientationMaskPortrait]);
        return UIInterfaceOrientationMaskPortrait;
    }

    
//    UIInterfaceOrientationMask mask = [SJRotationManager supportedInterfaceOrientationsForWindow:window];
//    NSLog(@"-----> %@", [self orientationString:mask]);
//    return mask;
}

// 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{

    return NO;
}

- (NSString*)orientationString:(UIInterfaceOrientationMask)orientation
{
    NSString *str = @"";
    switch (orientation) {
        case UIInterfaceOrientationMaskPortrait:
            str = @"支持竖屏";
            break;
        case UIInterfaceOrientationMaskLandscapeLeft:
            str = @"支持左横屏方向";
            break;
        case UIInterfaceOrientationMaskLandscapeRight:
            str = @"只支持右横屏方向";
            break;
        case UIInterfaceOrientationMaskPortraitUpsideDown:
            str = @"支持倒立竖直方向";
            break;
        case UIInterfaceOrientationMaskLandscape:
            str = @"支持左横屏和右横屏方向";
            break;
            
        case UIInterfaceOrientationMaskAll:
            str = @"支持所有方向";
            break;
        case UIInterfaceOrientationMaskAllButUpsideDown:
            str = @"支持竖屏和左右横屏";
            break;
        default:
            break;
    }
    
    return str;
}

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [[SDImageCache sharedImageCache] clearMemory];
}
@end
